<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-05 12:05:33
 * @LastEditTime: 2023-10-20 10:45:41
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Http\Services\Storage;

use App\Http\Services\Service;
use App\Repositories\MpcScrapRepository;
use App\Repositories\ProProcessesPassRepository;
use App\Repositories\ProProcessesWipRepository;
use Illuminate\Support\Facades\DB;

/**
 * @property ProProcessesWipRepository $wip
 * @author sunkaiyuan
 */
class ScrapService extends Service
    {

    public function boot(MpcScrapRepository $Repository, ProProcessesWipRepository $wip)
        {
        $this->repository = $Repository;
        $this->wip        = $wip;
        }
    public function list()
        {
        $data = $this->repository->condition(request())->getGrid();
        return $data;
        }
    protected function save($data = null)
        {
        $data = $data ?? request()->toArray();
        if (is_numeric($data['id'])) {
            if ($this->repository->whereId($data['id'])->value("status_id") > 0) {
                fail("已审核单据禁止编辑！");
                }
            }
        return $this->repository->autosave($data);
        }

    protected function saveArray()
        {
        $array_data = request('details');
        $data       = [];
        foreach ($array_data as $v) {
            $data[] = $this->save($v);
            }
        return $data;
        }

    protected function audit()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()
            ->filter(function ($v)
                {
                if ($v->status_id != 0)
                    fail("单据:[{$v->code}]已审核！");
                if (isEmpty($v->feeding_ratio))
                    fail("单据:[{$v->code}]必须填写此缸投缸比！！用来计算原白纱净重");
                if (isEmpty($v->hank))
                    fail("单据:[{$v->code}]必须填写此纱绞数！！用来计算打包绞数和烘干10绞重");
                $v->useStorage("返工复修")->subStorage();
                if ($v->wip_id > 0)
                    $this->wip->restoreWip($v->wip_id, $v->quantity);
                else
                    $v->wip_id = $this->wip->getWip([
                        'dyelot'      => $v->dyelot,
                        'process_id'  => 14,
                        'pro_dept_id' => 3,
                    ], [
                        'pro_pot_id'       => 0,
                        'pro_plan_id'      => 0,
                        'dyelot'           => $v->dyelot,
                        'material_id'      => $v->material_id,
                        'color_id'         => $v->color_id,
                        'faultle_quantity' => $v->quantity,
                        'hank'             => $v->hank,
                        'lot_name'         => $v->lot->name,
                        'feeding_ratio'    => $v->feeding_ratio,
                        'process_id'       => 14,
                        'company_id'       => 31,
                        'outfactory_id'    => 31,
                        'is_scrap'         => 1,
                        'pro_dept_id'      => 3,
                    ])->id;
                auditSave($v);
                });
        }
    protected function noaudit()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()
            ->filter(function ($v)
                {
                if ($v->status_id != 1)
                    fail("单据:[{$v->code}]未审核！");
                $v->nosubStorage()->nouseStorage();
                $this->wip->subWipStorage($v->wip, $v->quantity);
                auditSave($v, false);
                });
        }

    protected function delete()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()
            ->filter(function ($row)
                {
                if ($row->status_id == 1)
                    fail("单据[{$row->code}]已审核！");
                $row->delete();
                });
        }
    }